package org.jiuling.paging.spring.convert;

import org.jiuling.basics.response.paging.Page;
import org.jiuling.basics.validation.IntegerVerify;

/**
 * 分页转换器
 * @author 紫灵
 * @date 2024年03月10日 23时41分09秒
 */
public class PageConvert {

    /** 私有构造方法，禁止创建对象 */
    private PageConvert(){}

    /**
     * Springjpa分页转分页
     * @param springJpaPage springjpa分页
     * @return 分页
     * @param <T> 转换泛型
     */
    public static <T> Page<T> springJpaPageToPage(org.springframework.data.domain.Page<?> springJpaPage){
        Page<T> page = new Page<>();
        int number = springJpaPage.getNumber();
        number = IntegerVerify.isBlank(number)?1:number;
        page.setPageNum(number);
        int size = springJpaPage.getSize();
        page.setPageSize(size);
        page.setTotal(springJpaPage.getTotalElements());
        long startRow = number == 1?1:(number-1L)*size+1;
        page.setStartRow(startRow);
        page.setEndRow(startRow-1);
        page.setPages(springJpaPage.getTotalPages());
        page.setPrePage(number-1);
        page.setNextPage(number+1);
        return page;
    }

}
